package com.zjitc.CountDown;

import com.zjitc.controller.UserController;
import com.zjitc.dao.CatDao;
import com.zjitc.service.CatService;
import com.zjitc.service.impl.CatServiceImpl;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.omg.CORBA.TIMEOUT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/5/17 15:51
 * @description:
 */

public class CountDown {


  private volatile int limitSec; //记录倒计时时间
  private int curSec;//记录倒计时当下时间
  private int catid;
  public  CountDown(int limitSec,Integer catid) throws InterruptedException {
    this.limitSec = limitSec;
    this.curSec = limitSec;
    this.catid = catid;
    System.out.println("count down form " + limitSec);

    ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
    exec.scheduleAtFixedRate(new Task(), 0, 1, TimeUnit.SECONDS);
    TimeUnit.SECONDS.sleep(limitSec);   //暂停本线程
    exec.shutdownNow();
    System.out.println("Time out！");
    int num = 0;
    if (curSec<=0){

      return ;
    }
  }
@Component
  private class Task implements Runnable {


    public void run() {
      System.out.println("Time remains " + --curSec + " s");
/*      int num = 0;
      if (curSec<=0){
        catDao.changeCatSell(num,catid);
      }*/
    }
  }
  //Test
/*  public static void main(String[] args) throws InterruptedException{
        new CountDown(10);
    }*/
}
